import FSPSyntaxChecker as SYN
import FSPtoIFSPGenerator as IFSP
import FSPVariableResolution as VAR
from FSPNamespace import FSPNamespace
from indent import indent
import Exceptions as E

def test(sourceFile):
	source = (open(sourceFile, "r")).read()

	#syntax check
	#definition reordering
	syntaxChecker = SYN.FSPSyntaxChecker(source)
	FSPdefinitions = [d for d in syntaxChecker]
	print("~::Definizioni checkate e riordinate::~")
	for d in FSPdefinitions:
		pass
		print(indent(str(d))+"\n")
	
	#global context creation (namespace Global)
	#constant folding
	#constant propagation
	#localProcess, when, actionLabel, actionLabels, prefixActions and actionPrefix unrolling
	#variable templatization
	IFSPGenerator = IFSP.FSPtoIFSPGenerator(FSPdefinitions)
	IFSPdefinitions = [d for d in IFSPGenerator]
	print("~::Intermediate FSP 1::~")
	for d in IFSPdefinitions:
		pass
		print(indent(str(d))+"\n")
		
	#local contexts creation (nested namespaces)
	#localProcess and actionPrefix unrolling through variable template instantiation
	#variable template instantiation using "namespace views"
	varResolutor = VAR.FSPVariableSolver(IFSPdefinitions)
	IFSPdefinitions2 = [d for d in varResolutor]
	print("~::Intermediate FSP 2::~")
	for d in IFSPdefinitions2:
		pass
		print(indent(str(d))+"\n")
	
	#constant folding
	#localProcess, when, actionLabel, actionLabels, prefixActions and actionPrefix unrolling
	BFSPGenerator = IFSP.FSPtoIFSPGenerator(IFSPdefinitions2, skipLowercaseExpressions=True)
	BFSPdefinitions = [d for d in BFSPGenerator]
	print("~::Basic FSP::~")
	for d in BFSPdefinitions:
		print("\n" if  isinstance(d, FSPNamespace) else indent(str(d))+"\n")
